﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LearnDrawing.Controls
{
    /// <summary>
    /// WithGDI.xaml 的交互逻辑
    /// </summary>
    public partial class WithGDI : UserControl
    {
        public WithGDI()
        {
            InitializeComponent();
        }

        public string DrawingTime
        {
            get { return (string)GetValue(DrawingTimeProperty); }
            set { SetValue(DrawingTimeProperty, value); }
        }

        public static readonly DependencyProperty DrawingTimeProperty =
            DependencyProperty.Register("DrawingTime", typeof(string), typeof(WithGDI), new UIPropertyMetadata("", null));


        public WaveData WaveData
        {
            get { return (WaveData)GetValue(WaveDataDataProperty); }
            set { SetValue(WaveDataDataProperty, value); }
        }

        public static readonly DependencyProperty WaveDataDataProperty =
            DependencyProperty.Register("WaveData", typeof(WaveData), typeof(WithGDI), new UIPropertyMetadata(null, WaveDataPropertyChanged));

        private static void WaveDataPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs arg)
        {
            WithGDI? control = obj as WithGDI;
            WaveData Data = (WaveData)arg.NewValue;

            if (control != null && Data != null)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                control?.DrawWaveImage(Data);
                stopwatch.Stop();
                control.DrawingTime = $"Using:{stopwatch.ElapsedMilliseconds:D2}ms(WithGDI)";
            }
        }

        private void DrawWaveImage(WaveData Data)
        {
            int PicWidth = 1000;
            int PicHeight = 400;


            var wBitmap = new WriteableBitmap(PicWidth, PicHeight, 96, 96, PixelFormats.Bgr24, null);
            this.imageWave.Source = wBitmap;
            wBitmap.Lock();

            System.Drawing.Bitmap backBitmap = new System.Drawing.Bitmap(PicWidth, PicHeight, wBitmap.BackBufferStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, wBitmap.BackBuffer);
            using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(backBitmap))
            {
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;

                //绘制外框
                graphics.DrawRectangle(new System.Drawing.Pen(System.Drawing.Brushes.DarkGray, 1), new System.Drawing.Rectangle(0, 0, 1000, 400));

                //判定数据有效性
                if (Data == null)
                {
                    return;
                }

                System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Brushes.LightGreen);
                for (int i = 0; i < 1900; i++)
                {
                    int x1 = i;
                    int y1 = Data.Datas[i];

                    graphics.DrawEllipse(pen, new System.Drawing.Rectangle(new System.Drawing.Point(x1 / 2, y1), new System.Drawing.Size(60, 60)));
                }

                graphics.Flush();
                graphics.Dispose();
                backBitmap.Dispose();
            }
            wBitmap.AddDirtyRect(new Int32Rect(0, 0, PicWidth, PicHeight));
            wBitmap.Unlock();
        }
    }
}
